#region Copyright (c) 2006-2012 nHydrate.org, All Rights Reserved
// --------------------------------------------------------------------- *
//                           NHYDRATE.ORG                                *
//              Copyright (c) 2006-2012 All Rights reserved              *
//                                                                       *
//                                                                       *
// This file and its contents are protected by United States and         *
// International copyright laws.  Unauthorized reproduction and/or       *
// distribution of all or any portion of the code contained herein       *
// is strictly prohibited and will result in severe civil and criminal   *
// penalties.  Any violations of this copyright will be prosecuted       *
// to the fullest extent possible under law.                             *
//                                                                       *
// THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
// TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
// TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
// CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
// THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF THE NHYDRATE GROUP *
//                                                                       *
// UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
// PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
// SUBSTANTIALLY THE SAME, FUNCTIONALITY AS THIS PRODUCT                 *
//                                                                       *
// THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
// CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF NHYDRATE,          *
// THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO             *
// INSURE ITS CONFIDENTIALITY.                                           *
//                                                                       *
// THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
// PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
// EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
// THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
// SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                       *
// THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
// --------------------------------------------------------------------- *
#endregion
using System;
using System.Windows.Forms;

namespace nHydrate.Generator.Common.Forms
{
	public class ListViewComparer : System.Collections.IComparer
	{
		#region Class Members

		private readonly Array _typeList = null;
		private readonly ListView _listView = null;
		protected int _column = -1;

		#endregion

		#region Constructor

		public ListViewComparer(ListView listView, Array typeList)
		{
			_listView = listView;
			_listView.ColumnClick += new ColumnClickEventHandler(ErrorControl_ColumnClick);
			_typeList = typeList;
		}

		#endregion

		#region Property Implementations

		public int Column
		{
			get { return _column; }
			set { _column = value; }
		}

		protected Array TypeList
		{
			get { return _typeList; }
		}

		protected ListView ListView
		{
			get { return _listView; }
		}

		#endregion

		#region IComparer Members

		public int Compare(object x, object y)
		{
			if (this.Column == -1)
				return 0;

			var item1 = (ListViewItem)x;
			var item2 = (ListViewItem)y;

			var type = typeof(string);
			if ((0 < this.Column) && (this.Column < this.TypeList.Length))
				type = (System.Type)this.TypeList.GetValue(this.Column);

			if (type == typeof(int))
			{
				try
				{
					var int1 = int.Parse(item1.SubItems[this.Column].Text);
					var int2 = int.Parse(item2.SubItems[this.Column].Text);
					if (int1 == int2)
						return 0;
					else if (this.ListView.Sorting == SortOrder.Ascending)
						return int1 < int2 ? -1 : 1;
					else
						return int1 > int2 ? -1 : 1;
				}
				catch { }
			}
			//else if (type == typeof(MessageTypeConstants))
			//{
			//}
			else //All other cases are strings
			{
				var text1 = item1.SubItems[this.Column].Text;
				var text2 = item2.SubItems[this.Column].Text;
				if (text1 == text2)
					return 0;
				else if (this.ListView.Sorting == SortOrder.Ascending)
					return string.Compare(text1, text2, true);
				else
					return -string.Compare(text1, text2, true);
			}

			return 0;

		}

		#endregion

		#region Event Handlers

		private void ErrorControl_ColumnClick(object sender, ColumnClickEventArgs e)
		{
			if (e.Column == this.Column)
			{
				if (this.ListView.Sorting == SortOrder.Descending)
					this.ListView.Sorting = SortOrder.Ascending;
				else
					this.ListView.Sorting = SortOrder.Descending;
			}
			else
			{
				this.Column = e.Column;
				this.ListView.Sorting = SortOrder.Ascending;
			}
			this.ListView.Sort();
		}

		#endregion

	}
}